package cn.school.service;

import cn.school.dao.GradeDao;
import cn.school.entity.Grade;
import cn.school.entity.Page;
import cn.school.entity.Page2;
import cn.school.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class GradeService {
    public static final int PAGE_SIZE = 5;

    public java.util.List<Grade> findALL() {
        SqlSession s = MyBatisUtil.get();
        try {
            GradeDao gdo = s.getMapper(GradeDao.class);
            return gdo.findALL();
        } finally {
            MyBatisUtil.close(s);
        }
    }

    public Page<Grade> search(int pageNo, int pageSize, String gname)
    {
        SqlSession s = MyBatisUtil.get();
        try {
            GradeDao gdo = s.getMapper(GradeDao.class);
            List<Grade> search = gdo.search(pageNo, pageSize, gname);
            int total=gdo.searchTotal(gname);
            return new Page<>(total,search,pageNo);
        } finally {
            MyBatisUtil.close(s);
        }
    }

//    public Page2<Grade> search(int pageNo)
//    {
//        List<Grade> l=search0(pageNo);
//        int total=searchTotal();
//        return new Page2<Grade>(pageNo,total,PAGE_SIZE,l);
//    }
//    public List<Grade> search0(int pageNo){
//        SqlSession s = MyBatisUtil.get();
//        if(pageNo<1)
//            pageNo=1;
//        try {
//            GradeDao gdo = s.getMapper(GradeDao.class);
//            return gdo.search(pageNo,PAGE_SIZE);
//        } finally {
//            MyBatisUtil.close(s);
//        }
//    }

    /**
     * 获得页数
     * @return
     */
//    public int  searchTotal(){
//        SqlSession s = MyBatisUtil.get();
//
//        try {
//            GradeDao gdo = s.getMapper(GradeDao.class);
//            int lines= gdo.searchTotal();
//            return lines;//(int) Math.ceil(lines*1.0/PAGE_SIZE);
//        } finally {
//            MyBatisUtil.close(s);
//        }
//    }
}
